package com.example.mall.service;

import com.example.mall.dao.AccountDao;
import com.example.mall.dao.AddressDao;
import com.example.mall.dao.PersonInfoDao;
import com.example.mall.dto.response.BaseResponse;
import com.example.mall.model.Account;
import com.example.mall.model.Address;
import com.example.mall.model.PersonInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PersonInfoSerivce {

    @Autowired
    private PersonInfoDao personInfoDao;
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private AddressDao addressDao;

    public boolean updateAvatl(Integer aID, String newAvatl) {
        Account account = accountDao.getById(aID);
        if (account == null)
            return false;

        PersonInfo personInfo = personInfoDao.getById(account.getpID());
        personInfo.setAvatl(newAvatl);
        int result = personInfoDao.updateAvatlById(personInfo);
        return result == 1;
    }

    public boolean updateSex(Integer aID, Short newSex) {
        Account account = accountDao.getById(aID);
        if (account == null)
            return false;

        PersonInfo personInfo = personInfoDao.getById(account.getpID());
        personInfo.setSex(newSex);
        int result = personInfoDao.updateAvatlById(personInfo);
        return result == 1;
    }

    public boolean updateUserName(Integer aID, String newUserName) {
        Account account = accountDao.getById(aID);
        if (account == null)
            return false;

        account.setUsername(newUserName);
        int result = accountDao.updateUserNameByID(account);
        return result == 1;
    }

    //添加收货地址
    public void addAddress(Integer aID, String name, String phoneNumber, String location) {
        Account account = accountDao.getById(aID);
        Integer pID = account.getpID();
        Address address = new Address();
        address.setpID(pID);
        address.setName(name);
        address.setPhonenumber(phoneNumber);
        address.setLocation(location);
        addressDao.insertInfo(address);
    }

    //删除收货地址
    public void deleteAddress(Integer addressID) {
        addressDao.delete(addressID);
    }

    //更改收货地址
    public boolean updateAddress(Integer addressID, String name, String phoneNumber, String location) {
        Address address = addressDao.getById(addressID);
        if (address == null)
            return false;

        address.setName(name);
        address.setPhonenumber(phoneNumber);
        address.setLocation(location);
        addressDao.update(address);
        return true;
    }

}
